package com.citycloud.ccuap.tc.oauth2.config.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.citycloud.ccuap.tc.common.result.ResultObject;
import com.citycloud.ccuap.tc.oauth2.config.CustomerProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class CustomerAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		Object returnPage = request.getAttribute("returnPage");
		if (null != returnPage && returnPage.equals(true)) {
			redirectStrategy.sendRedirect(request, response, CustomerProperties.authenticationFrontendUrl);
		}

		response.setContentType("application/json; charset=utf-8");
		ObjectMapper mapper = new ObjectMapper();
		response.getWriter().print(mapper.writeValueAsString(ResultObject.getInstance().successMsg("登陆成功")));
	}
}
